import { getCurrentInstance, ref } from 'vue'
import { useUniAppSystemRectInfo } from '@tuniao/tnui-vue3-uniapp/hooks'
import { tnNavPage } from '@tuniao/tnui-vue3-uniapp/utils'
import type { StoreUser } from '@/types/store/user'
import type { NavbarRectInfo } from '@tuniao/tnui-vue3-uniapp/components/navbar'
import { generateRandomNumber } from '@/utils/local-mock'
import { useScrollTransparentNavbar } from '@/hooks'
import { getScenes } from '@/services/api'
import { onLoad, onShow } from '@dcloudio/uni-app'
import storageApi from '@/utils/storage'

export const useSubPage = () => {
  const instance = getCurrentInstance()
  const { navBarInfo } = useUniAppSystemRectInfo()
  const colorList = [
    {
      bgColor: '(255,213,128,0.4)',
      color: '#BE6811',
    },
    {
      bgColor: '(211,202,255,0.4)',
      color: '#8A7ED7',
    },
    {
      bgColor: '(179,207,255,0.4)',
      color: '#50B5FF',
    },
    {
      bgColor: '(255,198,178,0.4)',
      color: '#DA977F',
    },
    {
      bgColor: '(255,179,204,0.4)',
      color: '#E85E8D',
    },
    {
      bgColor: '(138,230,199,0.4)',
      color: '#73B09C',
    },
  ]
  // 用户信息
  const userInfo = ref<StoreUser>()

  const list = ref([])

  // 点击获取用户信息
  const loginHandle = () => {
    setTimeout(() => {
      // uni.hideLoading()
      userInfo.value = {
        uid: '2024-03-21 09:00:00',
        nickname: '英语AI',
        username: '',
        avatar: 'https://resource.tuniaokj.com/images/avatar/xiong.jpg',
        sex: 0,
        phone: '',
        desc: '',
        birthday: '',
        profession: '',
      }
    }, 0)
  }
  loginHandle()
  // 跳转到个人信息页面
  const navUserInfoPage = () => {
    tnNavPage('/info-page/user-info/index')
  }

  // 跳转到常见问题页面
  const navCommonProblemPage = () => {
    tnNavPage('/info-page/common-problem/index')
  }

  // 跳转到会员权益页面
  const navMemberRightsPage = () => {
    tnNavPage('/info-page/member-rights/index')
  }

  // 跳转到订单列表页面
  const navOrderListPage = () => {
    tnNavPage('/order-page/order-list/index')
  }

  onShow(() => {
    getData()
  })

  const getData = () => {
    let params = {
      page: 0,
      pageSize: 9999,
      grade: storageApi.get('gradeData').type,
    }
    getScenes(params).then((res) => {
      const { code, data } = res
      if (code === 0) {
        data.map((item, index) => {
          colorList.map((citem, cindex) => {
            if (index == cindex) {
              item.color = citem.color
              item.bgColor = citem.bgColor
            }
          })
        })
        list.value = data
      }
    })
  }
  const {
    triggerElementId,
    navbarOpacity,
    init: initTransparentScroll,
    updateTargetTriggerValue,
    opacityScrollHandle,
  } = useScrollTransparentNavbar(instance)

  // 顶部导航栏初始化完成事件
  const navbarInitFinishHandle = (info: NavbarRectInfo) => {
    updateTargetTriggerValue(info.height)
  }
  // useSubPageProvide(4)

  return {
    navBarInfo,
    userInfo,
    loginHandle,
    navUserInfoPage,
    navCommonProblemPage,
    navMemberRightsPage,
    navOrderListPage,
    initTransparentScroll,
    opacityScrollHandle,
    navbarInitFinishHandle,
    getData,
    list,
  }
}
